package com.naiterui.ehp.bs.b2c.pharmacy.controller.inner;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BaseExceptionCodes;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bp.support.mvc.RequestHeaderDecode;
import com.naiterui.ehp.bs.b2c.inventory.exception.StockServiceException;
import com.naiterui.ehp.bs.b2c.pharmacy.bo.WarehouseGoodsInventoryBO;
import com.naiterui.ehp.bs.b2c.pharmacy.service.IWarehouseGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Set;

@Slf4j
@Api(tags = {SwaggerTag.INNER_API + SwaggerTag.MEDICINE_DRUG, SwaggerTag.DS + SwaggerTag.MEDICINE_DRUG})
@Validated
@RestController
@RequestMapping("b2c/inner/cms/warehouse/goods")
public class WarehouseGoodsController {

    @Autowired
    private IWarehouseGoodsService warehouseGoodsService;

    /**
     * 删除选中商品
     */
    @ApiOperation(value = "删除选中商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warehouseId", value = "药店ID", paramType = "path", dataType = "long", required = true),
            @ApiImplicitParam(name = "skuIds", value = "药品SKUID", paramType = "body", dataType = "array", required = true)})
    @DeleteMapping("/{warehouseId}")
    public ResponseVO<Void> delSelectList(@PathVariable("warehouseId") Long warehouseId, @RequestBody Set<Long> skuIds,
                                          @RequestHeaderDecode String operator) throws StockServiceException {
        log.info("删除选中商品 请求参数：warehouseId = {}, skuIds = {}, operator = {}", warehouseId, skuIds, operator);
        this.warehouseGoodsService.delSelectList(warehouseId, skuIds, operator);
        return PublicService.returnResponseVO(BaseExceptionCodes.SUCCESS);
    }

    /**
     * 设置选中商品
     */
    @ApiOperation(value = "设置选中商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warehouseId", value = "药店ID", paramType = "path", dataType = "long", required = true),
            @ApiImplicitParam(name = "skuIds", value = "药品SKUID", paramType = "body", dataType = "array", required = true)})
    @PostMapping("/{warehouseId}")
    public ResponseVO<Void> setSelectList(@PathVariable("warehouseId") Long warehouseId, @RequestBody Set<Long> skuIds,
                                          @RequestHeaderDecode String operator) {
        log.info("设置选中商品 请求参数：warehouseId = {}, skuIds = {}, operator = {}", warehouseId, skuIds, operator);
        this.warehouseGoodsService.setSelectList(warehouseId, skuIds, operator);
        return PublicService.returnResponseVO(BaseExceptionCodes.SUCCESS);
    }

    /**
     * 设置库存
     */
    @ApiOperation(value = "设置库存")
    @ApiImplicitParam(name = "warehouseId", value = "药店ID", paramType = "path", dataType = "long", required = true)
    @PostMapping("/inventory/{warehouseId}")
    public ResponseVO<Void> updateInventory(@PathVariable("warehouseId") Long warehouseId,
                                            @Valid @RequestBody Set<WarehouseGoodsInventoryBO> warehouseGoodsInventorys,
                                            @RequestHeaderDecode String operator) throws StockServiceException {
        log.info("设置库存 请求参数：warehouseId = {}, warehouseGoodsInventorys = {}, operator = {}", warehouseId, warehouseGoodsInventorys, operator);
        this.warehouseGoodsService.updateInventory(warehouseId, warehouseGoodsInventorys, operator);
        return PublicService.returnResponseVO(BaseExceptionCodes.SUCCESS);
    }

}
